package com.example.demo;


import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController()
@RequestMapping("/path")
public class Controller {
    @RequestMapping("/sayHi")
    public String hello() {
        return "hello";
    }
    @RequestMapping(value = "p1",method = RequestMethod.GET)
    public String p1() {
        return "p1";
    }
    @PostMapping("/p2")
    public String p2() {
        return "p2";
    }
    @RequestMapping("/p3")
    public String p3(String name) {
        return name;
    }

    // 传递对象
    @RequestMapping("/p4")
    public String p4(User user) {
        return "user:" + user;
    }

    // 重命名
    @RequestMapping("/p5")
    public String p5(@RequestParam("userName") String name) {
        return name;
    }

    // 传递数组
    @RequestMapping("/p6")
    public String p6(String[] name) {
        return "name:" + List.of(name);
    }

    // 传递列表
    @RequestMapping("/p7")
    public String p7(@RequestParam List<String> list) {
        return "name:" + list;
    }
    // 传递 json
    @RequestMapping("/p8")
    public String p8(@RequestBody User user) {
        return "user:" + user;
    }

    // 获取 url 的值
    @RequestMapping("/p9/{name}")
    public String p9(@PathVariable String name) {
        return "name:" + name;
    }

    @RequestMapping("/p10/{name}/{userAge}")
    public String p10(@PathVariable String name,@PathVariable("userAge") String age) {
        return "name:" + name + ", age:" + age;
    }

    // 传递文件
    @RequestMapping("/p11")
    public String p11(@RequestPart("fileName") MultipartFile file) {
        return "file:" + file.getOriginalFilename();
    }

    @Autowired
    private UserService service;

    @RequestMapping("/p12")
    public void test() {
        service.say();
    }


    @Value("${my.key}")
    private String myKey;
    @PostConstruct //这个注解的作用就是在类加载的时候就执行下面方法
    @RequestMapping("/p13")
    public String readValue() {
        System.out.println(myKey);
        return myKey;
    }
//    @Qualifier("student1")
//    @Autowired
//    private Student student1;
//    @RequestMapping("/p14")
//    public String readBean1() {
//        return student1.toString();
//    }

    @Autowired
    private Student student;
    @RequestMapping("/p14")
    public String readBean() {
        return student.toString();
    }
}
